package com.example.agricultural_machinery_navigation.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.agricultural_machinery_navigation.R;
import com.example.agricultural_machinery_navigation.bean.RecycleViewRootInfo;

import java.util.List;

/*******
 * 类名：RootRecycleViewAapter
 * 功能：外层recycleview布局适配器类
 * ********/
public class RootRecycleViewAapter extends RecyclerView.Adapter<RootRecycleViewAapter.MyViewHolder> {
     private Context mContext;
     private List<RecycleViewRootInfo> mData;
     private LayoutInflater inflater;
     private MyRecycleViewAapter myRecycleViewAapter ;
     /*****
      * 方法名： RootRecycleViewAapter
      * 功能：构造方法
      * @param context  上下文对象
      * @param data  要填充的item数据
      * ******/
    public RootRecycleViewAapter(Context context , List<RecycleViewRootInfo> data) {
        mContext = context;
        mData = data;
        inflater = LayoutInflater.from(context);
    }

    /*******
     * 方法名：onCreateViewHolder
     * 功能：初始化item布局
     * @param parent item父容器
     * @param  viewType 标志位
     *
     * *******/
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.root_recycleview_list_item_layout,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }



    /*******
     * 方法名：onBindViewHolder
     * 功能：绑定布局的子控件
     * @param holder viewholder对象，封装了布局控件参数
     * @param  position  当前item的位置
     * **********/
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder  holder, int position) {
        //设置功能区的标题
        holder.textView.setText(mData.get(position).getTitle());
        //设置布局管理类
        myRecycleViewAapter =new MyRecycleViewAapter(mContext,mData.get(position).getFunction());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext,3);
        holder.recyclerView.setLayoutManager(gridLayoutManager);
        //设置适配器
        holder.recyclerView.setAdapter(myRecycleViewAapter);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    /*****
     * 方法名： MyViewHolder
     * 功能： 自定义的一个RecyclerView.ViewHolder 子类 用来封装item子控件
     * *****/
    protected class  MyViewHolder extends  RecyclerView.ViewHolder {
        TextView textView ;
        RecyclerView recyclerView;

        /*****
         * 方法名：MyViewHolder
         * 功能：构造方法
         * @param  itemView 当前item的布局view
         * ******/
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.recycleview_list_item_title_tv);
            recyclerView =  itemView.findViewById(R.id.recycleview_list_item_function_button_rv);
        }
    }
}
